package sctu.edu.java.year2016.team07.project.client;

import sctu.edu.java.year2016.team03.liyouhong.day20160531.chapter10_1.Add;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * Created by Huang Suixin on 2016/6/26.
 */
public class GetUserInformationFrame extends JDialog {
    public GetUserInformationFrame(User user,String question) {
        Container con = getContentPane();
        JPanel pan = new JPanel();
        pan.setOpaque(false);       //设置透明
        pan.setLayout(new GridLayout(4, 0));

        JPanel namepan = new JPanel();
        namepan.setOpaque(false);
        JPanel Telpan = new JPanel();
        Telpan.setOpaque(false);
        JPanel Addpan = new JPanel();
        Addpan.setOpaque(false);
        JPanel buttonpan = new JPanel();
        buttonpan.setOpaque(false);
        //新建组件
        JLabel nameLabel = new JLabel("姓名：");
        nameLabel.setFont(new Font("dialog", 1, 20));
        JLabel TelLabel = new JLabel("电话：");
        TelLabel.setFont(new Font("dialog", 1, 20));
        JLabel AddLabel = new JLabel("住址：");
        AddLabel.setFont(new Font("dialog", 1, 20));
        JTextField name = new JTextField(10);
        JTextField Tel = new JTextField(10);
        JTextField Add = new JTextField(15);
        JButton button = new JButton("提交");
        button.setFont(new Font("dialog", 1, 20));
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        //添加组件
        namepan.add(nameLabel);
        namepan.add(name);
        Telpan.add(TelLabel);
        Telpan.add(Tel);
        Addpan.add(AddLabel);
        Addpan.add(Add);
        buttonpan.add(button);

        pan.add(namepan);
        pan.add(Telpan);
        pan.add(Addpan);
        pan.add(buttonpan);
        con.add(pan);

        //为按钮添加事件
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                user.setName(name.getText());
                user.setTel(Tel.getText());
                user.setAdd(Add.getText());
                user.record(question);
                dispose();

                //添加反馈成功弹窗提醒
                JDialog recordok = new JDialog();
                Container con1 = recordok.getContentPane();
                JPanel panel = new JPanel();
                JLabel jl = new JLabel("您的反馈已经记录成功！");
                jl.setFont(new Font("dialog",1,20));
                JButton ok = new JButton("ok");
                ok.setFont(new Font("dialog", 1, 20));
                ok.setContentAreaFilled(false);
                ok.setBorderPainted(false);

                panel.add(jl);
                panel.add(ok);
                con1.add(panel);

                ok.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        recordok.dispose();
                    }
                });
                recordok.setTitle("您的反馈已经记录成功！");
                recordok.setLocation(con.getWidth(),con.getHeight());
                recordok.setSize(300,100);
                recordok.setResizable(false);
                recordok.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                recordok.setAlwaysOnTop(true);
                recordok.setVisible(true);
            }
        });


        //初始化
        setTitle("反馈，请输入您的基本信息");
        setAlwaysOnTop(true);
        setSize(450, 250);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setVisible(true);

        //设置窗口背景
        try {
            ImageIcon background = new ImageIcon(ImageIO.read((new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\image\\land_background.jpg"))));
            JLabel label = new JLabel(background);
            label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
            JPanel imagePanel = (JPanel) this.getContentPane();     //将内容窗格式转化成JPanel，否则不能使用方法setOpaque()来使内容窗格透明
            imagePanel.setOpaque(false);         //使用方法setOpaque()来是内容窗格透明
            imagePanel.setLayout(new FlowLayout());
            getLayeredPane().setLayout(null);

            getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
